#!/usr/bin/env python
#-*-coding:utf-8-*-

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This project is sponsored by:
#   http://www.equiposcreativos.com/

################################################################################
# META
################################################################################

__version__ = "0.2"
__license__ = "GPL3"
__author__ = "JBC"
__since__ = "0.2"
__date__ = "2010/09/05"


################################################################################
# IMPORTS
################################################################################

import sys
import json

from lib.xgoogle import translate

import search
import stopwords
import feeds


################################################################################
# FUNCTIONS
################################################################################

def main(jsonstr):
    
    response = {}
    
    try:
        data = json.loads(jsonstr)
        cmd = data["cmd"]
        
        if cmd == "add_feed":
            feed = data["feed"]
            feeds.register(feed)

        elif cmd == "remove_feed":
            feed = data["feed"]
            feeds.remove(feed)
        
        elif cmd == "add_stopword":
            word = data["word"]
            stopwords.register(word)

        elif cmd == "remove_stopword":
            word = data["word"]
            stopwords.remove(word)
        
        elif cmd == "search":
            query = data["query"]
            toLang = data["toLang"]
            replace = data["replace"]
            agent = data["agent"]
            entries = search.search(query, toLang, replace, agent)
            response["entries"] = entries
            
        elif cmd == "status":
            response["stopwords"] = stopwords.get_stopwords()
            response["feeds"] = feeds.get_feeds()
            response["langs"] = translate._languages
        
        else:
            msg = "Unknow command '%s'" % cmd
            raise Exception(msg)
        
    except Exception as err:
        response["error"] = str(err)
        response["status"] = "error"
    
    else:
        response["status"] = "ok"
    
    response.update({"request": jsonstr})
    
    return json.dumps(response)
    

################################################################################
# MAIN
################################################################################

if __name__ == "__main__":
    
    if len(sys.argv) != 2:
        raise Exception("Invalid number of arguments")
        
    response = main(sys.argv[1])
        
    print response
    
    
